package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemGoodsDetailItemRecommendBinding;
import com.sgb.kjwl.model.entity.user.RecommendListEntity;
import com.sgb.kjwl.utils.PointUtils;

import java.util.List;

/**
 * @author qiangHeNoooo
 * 说明：商品详情页面的底部商品推荐
 * 时间：2021.08.18
 */
public class GoodsRecommendAdapter extends BaseQuickAdapter<RecommendListEntity, BaseViewHolder> {
    private Context context;
    private ResembleGoodsClick mResembleGoodsClick;

    public void setResembleGoodsClick(ResembleGoodsClick mResembleGoodsClick) {
        this.mResembleGoodsClick = mResembleGoodsClick;
    }

    public GoodsRecommendAdapter(Context context, @Nullable @org.jetbrains.annotations.Nullable List<RecommendListEntity> data) {
        super(R.layout.item_goods_detail_item_recommend, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder holder, RecommendListEntity entity) {
        ItemGoodsDetailItemRecommendBinding binding = DataBindingUtil.bind(holder.itemView);
        if (binding == null) return;
        binding.setVariable(BR.viewModel, entity);
        binding.itemContainer.setOnClickListener(view -> {
            if (mResembleGoodsClick != null) {
                mResembleGoodsClick.resembleGoodsClick(entity);
            }
        });
        if ("1".equals(entity.getSelfSupportTag())) {
            binding.itemImageZiying.setVisibility(View.VISIBLE);
        } else {
            binding.itemImageZiying.setVisibility(View.INVISIBLE);
        }

        if (!TextUtils.isEmpty(entity.getVipPrice()) && Double.valueOf(entity.getVipPrice()) > 0) {
            if (entity.getPromoteStatus() == 2) {
                holder.setVisible(R.id.item_tag_cuxiao, true);
                if (Double.valueOf(entity.getVipPrice())>Double.valueOf(entity.getPromotePrice())) {
                    holder.setVisible(R.id.tag_vip, false);
                    binding.itemTvPrice.setText("¥" + entity.getPromotePrice() + "/" + entity.getUnit());
                    binding.itemTvPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
                }else {
                    holder.setVisible(R.id.tag_vip, true);
                    binding.itemTvPrice.setText(entity.getVipPrice());
                    binding.itemTvPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                }
            } else {
                holder.setVisible(R.id.tag_vip, true);
                binding.itemTvPrice.setText(entity.getVipPrice());
                binding.itemTvPrice.setTextColor(context.getResources().getColor(R.color.colorF7AB01));
                holder.setVisible(R.id.item_tag_cuxiao, false);
            }
        } else {
            holder.setVisible(R.id.tag_vip, false);
            if (entity.getPromoteStatus() == 2) {
                holder.setVisible(R.id.item_tag_cuxiao, true);
                binding.itemTvPrice.setText("¥" + entity.getPromotePrice() + "/" + entity.getUnit());
                binding.itemTvPrice.setTextColor(context.getResources().getColor(R.color.new_org2));
            }else {
                holder.setVisible(R.id.item_tag_cuxiao, false);
                binding.itemTvPrice.setText("¥" + entity.getPrice() + "/" + entity.getUnit());
                binding.itemTvPrice.setTextColor(context.getResources().getColor(R.color.new_org));
            }
        }
        Glide.with(context).load(entity.getImgUrl()).apply(new RequestOptions().placeholder(R.drawable.null_img_list).error(R.drawable.null_img_list)).into(binding.itemLogoView);
        binding.executePendingBindings();
    }

    public interface ResembleGoodsClick {
        /**
         * item
         *
         * @param recommendListEntity
         */
        void resembleGoodsClick(RecommendListEntity recommendListEntity);
    }
}
